package controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.BaseServlet;
import model.HumiTempBean;
import model.HumiTempDao;

/**
 * Servlet implementation class Redirect
 */
public class Redirect extends BaseServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Redirect() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("Redirecting");
		RequestDispatcher r = null;

		if (!isLoggedIn(request)) {
			response.sendRedirect("login");
			return;
		}
		if (request.getParameter("Home") != null) {
			if (request.getParameter("Home").equals("1")) {
				r = request.getRequestDispatcher("WEB-INF/content.jsp");
			}
		} else if (request.getParameter("Webcam") != null) {
			if (request.getParameter("Webcam").equals("1")) {
				r = request.getRequestDispatcher("WEB-INF/webcam.jsp");
			}
		} else if (request.getParameter("Useredit") != null) {
			if (request.getParameter("Useredit").equals("1")) {
				r = request.getRequestDispatcher("UserPage");
			}
		} else if (request.getParameter("Uitloggen") != null) {
			if (request.getParameter("Uitloggen").equals("1")) {
				request.getSession().invalidate();
				r = request.getRequestDispatcher("login");
			}
		}else if (request.getParameter("Graph") != null) {
			if (request.getParameter("Graph").equals("1")) {
				r = request.getRequestDispatcher("WEB-INF/chart.jsp");
			} else if (request.getParameter("Graph").equals("2")) {
				String s = request.getParameter("startdatum") + " 00:00:00";
				System.out.println("Startdatum: "+s);
				String e = request.getParameter("einddatum") + " 23:59:59";
				System.out.println("Einddatum: "+e);
				char t = request.getParameter("type").charAt(0);
				System.out.println("type: " + t);
				HumiTempDao ht = new HumiTempDao();
				long[] xvals = new long[0];
				double[] yvals = new double[0];
				ArrayList<HumiTempBean> beans = new ArrayList<HumiTempBean>();
				if(t == 'H'){
					beans = ht.getHumi(Timestamp.valueOf(s), Timestamp.valueOf(e));
					System.out.println(Timestamp.valueOf(s).toString());
				}else if(t == 'T'){
					beans = ht.getTemp(Timestamp.valueOf(s), Timestamp.valueOf(e));
					System.out.println(Timestamp.valueOf(s).toString());
				}
				xvals = new long[beans.size()];
				yvals = new double[beans.size()];
				for(int i = 0; i < beans.size(); i++){
					xvals[i] = beans.get(i).getTime().getTime();
					yvals[i] = beans.get(i).getValue();
				}
				request.setAttribute("xvals", xvals);
				request.setAttribute("yvals", yvals);
				r = request.getRequestDispatcher("WEB-INF/graph.jsp");
			}
		} else if (request.getParameter("Interval") != null) {
			if (request.getParameter("Interval").equals("1")) {
				r = request.getRequestDispatcher("WEB-INF/interval.jsp");
			}
		}  else if (request.getParameter("Help") != null) {
			if (request.getParameter("Help").equals("1")) {
				r = request.getRequestDispatcher("WEB-INF/Help.jsp");
			}
		} else {
			response.sendRedirect("login");
			System.out.println("Redirecting to content not found");
			return;			
		}
		r.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

	}

}
